package com.shop.apigateway.filter;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @author denpeng
 * @create 2018-05-22 11:15
 **/
@Component
public class TokenFilter extends ZuulFilter {

  @Override
  public String filterType() {
    return PRE_TYPE; //pre
  }

  @Override
  public int filterOrder() {
    return PRE_DECORATION_FILTER_ORDER - 1; //比prefilter小
  }

  @Override
  public boolean shouldFilter() {
    return true;
  }

  @Override
  public Object run() throws ZuulException {
    RequestContext requestContext = RequestContext.getCurrentContext();
    HttpServletRequest request = requestContext.getRequest();
    String token = request.getParameter("token");
    //从url中获取需要校验的参数
//    if (StringUtils.isBlank(token)) {
//      requestContext.setSendZuulResponse(false);
//      requestContext.setResponseStatusCode(401);
//    }

    return null;
  }
}
